<template>
  <div ref="chartContainer" class="base-chart"></div>
</template>

<script>
import * as echarts from "echarts";

export default {
  name: "BaseChart",
  props: {
    options: {
      type: Object,
      required: true,
    },
    theme: {
      type: String,
      default: "default",
    },
  },
  data() {
    return {
      chartInstance: null,
    };
  },
  watch: {
    options: {
      handler(newVal) {
        this.updateChart(newVal);
      },
      deep: true,
    },
  },
  mounted() {
    this.initChart();
    window.addEventListener("resize", this.handleResize);
  },
  beforeDestroy() {
    if (this.chartInstance) {
      this.chartInstance.dispose();
    }
    window.removeEventListener("resize", this.handleResize);
  },
  methods: {
    initChart() {
      if (this.$refs.chartContainer) {
        this.chartInstance = echarts.init(
          this.$refs.chartContainer,
          this.theme
        );
        this.chartInstance.setOption(this.options);
      }
    },
    updateChart(options) {
      if (this.chartInstance) {
        this.chartInstance.setOption(options);
      }
    },
    handleResize() {
      if (this.chartInstance) {
        this.chartInstance.resize();
      }
    },
  },
};
</script>

<style scoped>
.base-chart {
  width: 100%;
  height: 100%;
  min-height: 300px;
}
</style>